package Nonogram;

import java.io.File;
import java.io.FileNotFoundException;

/**
 * Nonogram
 * @author Rachel Wedewer, Mollie Burkle, Kristen Nielsen
 * Date Modified: 4/30/2014
 * 
 * This is the main class that controls the whole system and links the
 * four main components together: PuzzleManager, Data, View, and Solver.
 */
public class Nonogram {
    
    Data data;
    View view;
    Solver solver; 
    String puzzle;
    PuzzleManager pM;
    
    public Nonogram() throws FileNotFoundException {
    	pM = new PuzzleManager();
        //puzzle = "res/Block.txt";
        puzzle = (pM.getFirstPuzzle());
        data = new Data(this, puzzle);
        
        view = new View(this);
        solver = new Solver(this, data); 
   	    	
    }
    
    public void newGamePuzzle() throws FileNotFoundException{
        
    	pM = new PuzzleManager();
    	String selectedPuzzle = pM.getFileName();
    	if (selectedPuzzle == "NOPE"){
    		//Do nothing
    	}
    	else{
        
        puzzle = (selectedPuzzle);
        data = new Data(this,puzzle);
        
        view.dispose();
        view = new View(this);
        solver = new Solver(this, data);
        } 
    }
    
	public void importPuzzle() throws FileNotFoundException {
		pM = new PuzzleManager();
    	String selectedPuzzle = pM.getImportPuzzle();
    	if (selectedPuzzle == "NOPE"){
    		//Do nothing
    	}
    	else{
        
        puzzle = (selectedPuzzle);
        data = new Data(this,puzzle);
        
        view.dispose();
        view = new View(this);
        solver = new Solver(this, data);
        } 
		
	}
    
    public int getBoardSize(){
    	return data.boardsize;
    }
    
    
    
    public GridArray getPuzzleGridData(){
    	return data.getGrid();
    }
    
    public CluesArray getRowCluesData(){
    	return data.getRows();
    }
   
    
    public CluesArray getColsCluesData(){
    	return data.getCols();
    }
    
    public void updateGridGUI(){
    	view.updateGridGUI();
    }
    
    public int[] getRowLineClues(int line){
    	return getRowCluesData().getLineClues(line);
    	
    }
    
    public int[] getColLineClues(int line){
    	return getColsCluesData().getLineClues(line);
    }
        
    public void runSolver(){
    	solver.runSolver();
    	updateGridGUI();
    }
    
    public void runSolverAsLongAsPossible(){
    	solver.runSolverAsLongAsPossible();
    	view.updateGridGUI();
    }

	public void clearGrid(){
		data.getGrid().setGridAllUnknownState();
		view.updateGridGUI();
	}
    
    public void updateExplanation(String text){
    	view.addToExplainTextArea(text);
    }

	/**
     * @param args the command line arguments
	 * @throws FileNotFoundException 
     */
    public static void main(String[] args) throws FileNotFoundException {
    	Nonogram nono = new Nonogram();
    	
    }

	public void detectErrors() {
		boolean errors;
		errors = solver.ErrorDetection();
		
	}
 
}
